<template>
  <div class="app-container">
    <exception-filter @on-filter="submitFilterForm"/>
    <div style="padding: 20px 0">
      <el-button-group>
        <el-button v-if="roleCheck('batch-deal')" size="mini" type="primary" @click="multipleHandler">勾选批量处理转账
        </el-button>
      </el-button-group>
      <el-button-group>
        <el-button size="mini" type="primary" icon="el-icon-download" @click="exportHandle">导出
        </el-button>
      </el-button-group>
    </div>
    <el-table
      border
      :data="tableData.list"
      v-loading="loading"
      ref="multipleTable"
      element-loading-text="加载中..."
      @selection-change="handleSelectionChange"
      class="el-table-align-center">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column label="转账时间/转账单号" prop="createTime" width="160px" align="center">
        <template slot-scope="{row}">
          {{row.createTime | toTime}}
          <div>{{row.tradeId}}</div>
        </template>
      </el-table-column>
      <el-table-column label="账户类型" prop="userType" align="center">
        <template slot-scope="{row}">
          {{ row.userType | toConverter('ACCOUNT_TYPE')}}
        </template>
      </el-table-column>
      <el-table-column label="账户名" prop="username" align="center">
      </el-table-column>
      <!--      <el-table-column label="姓名" prop="reallyName" align="center">-->
      <!--      </el-table-column>-->
      <!--<el-table-column label="手机号" prop="telephone" align="center">-->
      <!--</el-table-column>-->
      <el-table-column label="游戏账户名" prop="gameUsername" align="center">
      </el-table-column>
      <el-table-column label="来自" prop="fromChannelId" align="center">
        <template slot-scope="{row}">
          {{ row.fromChannelId | toConverter('TRANSFER_PLATFORMS')}}
        </template>
      </el-table-column>
      <el-table-column label="转至" prop="toChannelId" align="center">
        <template slot-scope="{row}">
          {{ row.toChannelId | toConverter('TRANSFER_PLATFORMS')}}
        </template>
      </el-table-column>
      <el-table-column label="转账金额" prop="amount" align="center">
      </el-table-column>
      <el-table-column label="转账后中心钱包余额" prop="balance" align="center">
        <template slot-scope="{row}">
          {{ row.balance | currency}}
        </template>
      </el-table-column>
      <!--<el-table-column label="转账后平台钱包余额" prop="balance">-->
      <!--</el-table-column>-->
      <el-table-column label="更新时间" prop="lastUpdate" align="center">
        <template slot-scope="{row}">
          {{row.lastUpdate | toTime}}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template slot-scope="{row}">
          {{row.status | toConverter('TRANSFER_STATUS')}}
        </template>
      </el-table-column>
      <el-table-column label="处理人" prop="operator" align="center">
      </el-table-column>
      <el-table-column label="处理时间" prop="lastUpdate" align="center">
        <template slot-scope="{row}">
          {{row.lastUpdate | toTime}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <div v-if="roleCheck('single-deal')">
            <el-button
              type="warning"
              size="mini"
              v-if="row.status===0"
              @click="openDialog({dialogName: staticDialogName, props: row })"
            >处理异常转账
            </el-button>
            <div class="reset" v-else>
              <span >已处理</span> <br/>
              <el-button size="mini" type="primary"  @click="resetException(row.tradeId)" v-if="row.toChannelId===0&&row.status===2">重置
              </el-button>
            </div>
          </div>
          <span v-else>
            {{row.status===0?'待处理':'已处理'}}
          </span>
        </template>
      </el-table-column>
    </el-table>

    <exception-dialog
      v-if="dialogName === staticDialogName"
      v-model="dialogVisible"
      :id="dialogProps.id"
      @closeDialog="closeDialog"
      :multipleSelection="multipleSelection"
      :row-data="dialogProps"
      @submit="submitFinished"/>

    <div class="pagination-container">
      <el-pagination background @size-change="gridSizeChange" @current-change="gridCurrentChange"
                     :current-page="filters.page" :page-sizes="gridPageSize" :page-size="filters.size"
                     :layout="gridLayout" :total="tableData.total">
      </el-pagination>
    </div>
  </div>
</template>


<script>
import { getTransferExceptions, exportTransferExceptions, resetTransferExceptions } from '@/services/api/transfer';
import { grid, dialog } from '@/build';
import ExceptionDialog from '@/views/finance/transfer/exceptionDialog';
import ExceptionFilter from '@/views/finance/transfer/exceptionFilter';

export default {
  name: 'transfer-logs',
  components: {
    ExceptionFilter,
    ExceptionDialog,
  },
  mixins: [grid, dialog],
  data() {
    return {
      staticDialogName: 'exception-dialog',
      tableData: {},
      multipleSelection: [],
      filters: {
        time: [moment()
          .format('YYYY-MM-DD 00:00:00'), moment()
          .format('YYYY-MM-DD 23:59:59')],
        dateType: 1,
        size: 20,
        page: 1,
        startTime: moment()
          .format('YYYY-MM-DD 00:00:00'),
        endTime: moment()
          .format('YYYY-MM-DD 23:59:59'),
      },
      resetExc: {
        tradeId: undefined,
      },
    };
  },
  methods: {
    multipleHandler() {
      if (this.multipleSelection.length > 0) {
        this.openDialog({ dialogName: this.staticDialogName });
      } else {
        this.$message({
          message: '请选择批量处理数据',
          type: 'warning',
        });
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    closeDialog() {
      this.$refs.multipleTable.clearSelection();
      this.dialogVisible = false;
    },
    resetException(id) {
      this.$confirm('你确定要重置异常吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
        .then(() => {
          resetTransferExceptions({
            orderId: id,
          })
            .then(([result, data]) => {
              if (result) {
                this.doGet({ action: 'getGrid' });
                this.$message({
                  type: 'success',
                  message: '重置成功!',
                });
              } else {
                this.$message({
                  type: 'error',
                  message: '重置失败！',
                });
              }
            })
            .catch(() => {
            });
        });
    },
    exportHandle() {
      const {
        startTime, endTime, dateType, toChannel, fromChannel, minAmount, maxAmount, status, username, reallyName, telephone, tradeId,
      } = this.filters;
      const ajaxData = {
        startTime,
        endTime,
        dateType,
        toChannel,
        fromChannel,
        minAmount,
        maxAmount,
        status,
        username,
        reallyName,
        telephone,
        tradeId,
      };
      const filename = '异常转账记录明细报表.csv';
      const header = ['转账时间', '转账单号', '账户类型', '账户名', '姓名', '游戏账户名', '来自', '转至', '转账金额', '转账后中心钱包余额', '更新时间', '状态', '处理人', '处理时间'];
      const filterField = ['createTime', 'tradeId', 'userType', 'username', 'reallyName', 'gameUsername', 'fromChannelId', 'toChannelId', 'amount', 'balance', 'lastUpdate', 'status', 'operator', 'lastUpdate'];
      exportTransferExceptions(ajaxData)
        .then(([result, data]) => {
          if (result) {
            const exportData = this.responseFormatter(data.list);
            this.export2Excel(header, filterField, exportData, filename);
          } else {
            this.$message({
              message: `导出失败,${data.message}`,
              type: 'warning',
            });
          }
        });
    },
    responseFormatter(data) {
      data.map((item) => {
        item.createTime = _.toTime(item.createTime);
        item.userType = _.toConverter(item.userType, 'ACCOUNT_TYPE');
        item.amount = _.currency(item.amount);
        item.balance = _.currency(item.balance);
        // item.amountWithoutFee = _.currency(item.amountWithoutFee)
        item.lastUpdate = _.toTime(item.lastUpdate);
        item.status = _.toConverter(item.status, 'TRANSFER_STATUS');
        item.fromChannelId = _.toConverter(item.fromChannelId, 'TRANSFER_PLATFORMS');
        item.toChannelId = _.toConverter(item.toChannelId, 'TRANSFER_PLATFORMS');
      });

      return data;
    },
  },
  mounted() {
    this.bindGet(getTransferExceptions, 'tableData', {
      action: 'getGrid',
      sendingData: 'filters',
    });
    this.getAll();
  },
};
</script>

<style lang="scss" scoped>
.reset{
  /deep/ {
    .el-button--mini{
      padding:4px 12px;
    }
  }
}
</style>
